home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_07_01 / v7n1070a.txt < prev    next >
Text File  |  1989-02-13  |  3KB  |  195 lines

  1. /* Referencing arrays using array and pointer notation */
  2.  
  3. #include <stdio.h>
  4.  
  5. main()
  6. {
  7.     static char text[] = "ABCD";
  8.  
  9.     printf("text[0]     = %c\n", text[0]);
  10.     printf("*(text + 0) = %c\n", *(text + 0));
  11.     printf("*(0 + text) = %c\n", *(0 + text));
  12.     printf("*text       = %c\n", *text);
  13. }
  14.  
  15. text[0]     = A
  16. *(text + 0) = A
  17. *(0 + text) = A
  18. *text       = A
  19.  
  20.  
  21.  
  22.  
  23. /* Referencing pointers using pointer and array notation */
  24.  
  25. #include <stdio.h>
  26.  
  27. main()
  28. {
  29.     char *ptext = "ABCD";
  30.  
  31.     printf("*ptext       = %c\n", *ptext);
  32.     printf("*(ptext + 0) = %c\n", *(ptext + 0));
  33.     printf("*(0 + ptext) = %c\n", *(0 + ptext));
  34.     printf("ptext[0]     = %c\n", ptext[0]);
  35. }
  36.  
  37. *ptext       = A
  38. *(ptext + 0) = A
  39. *(0 + ptext) = A
  40. ptext[0]     = A
  41.  
  42.  
  43.  
  44.  
  45. #include <stdio.h>
  46.  
  47. main()
  48. {
  49.     static int i[5] = {1, 2, 3, 4, 5};
  50.     int *pi = &i[2];
  51.     int j;
  52.  
  53.     for (j = -2; j <= 2; ++j)
  54.         printf("pi[%2d] = %d\n", j, pi[j]);
  55.  
  56.  
  57.     {
  58.     char *table[] = {
  59.         "red",
  60.         "blue",
  61.         "green"
  62.     };
  63.  
  64.     printf("*table[1]       = %c\n", *table[1]);
  65.     printf("*(table[1] + 0) = %c\n", *(table[1] + 0));
  66.     printf("table[1][0]     = %c\n", table[1][0]);
  67.     printf("table[2][4]     = %c\n", table[2][4]);
  68.     }
  69. }
  70.  
  71. pi[-2] = 1
  72. pi[-1] = 2
  73. pi[ 0] = 3
  74. pi[ 1] = 4
  75. pi[ 2] = 5
  76. *table[1]       = b
  77. *(table[1] + 0) = b
  78. table[1][0]     = b
  79. table[2][4]     = n
  80.  
  81.  
  82.  
  83.  
  84.  
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87.  
  88. main()
  89. {
  90.     int *pi;
  91.     int i;
  92.  
  93.     pi = malloc(5);
  94.     if (pi == NULL) {
  95.         /* handle error */
  96.     }
  97.  
  98.     for (i = 0; i < 5; ++i) {
  99.         pi[i] = i * i;
  100.         printf("pi[%d] = %d\n", i, pi[i]);
  101.     }
  102. }
  103.  
  104. pi[0] = 0
  105. pi[1] = 1
  106. pi[2] = 4
  107. pi[3] = 9
  108. pi[4] = 16
  109.  
  110.  
  111.  
  112.  
  113.  
  114. #include <stdio.h>
  115.  
  116. main()
  117. {
  118.     char a[10][20];
  119.     void f(char [10][20]);
  120.  
  121.     f(a);
  122. }
  123.  
  124. void f(char a[10][20])
  125. {
  126.     printf("sizeof(a) = %lu\n", 
  127.         (unsigned long) sizeof(a));
  128.     printf("sizeof(*a) = %lu\n", 
  129.         (unsigned long) sizeof(*a));
  130.     printf("sizeof(a[0]) = %lu\n", 
  131.         (unsigned long) sizeof(a[0]));
  132.     printf("sizeof(a[0][0]) = %lu\n", 
  133.         (unsigned long) sizeof(a[0][0]));
  134. }
  135.  
  136. sizeof(a) = 4
  137. sizeof(*a) = 20
  138. sizeof(a[0]) = 20
  139. sizeof(a[0][0]) = 1
  140.  
  141.  
  142.  
  143.  
  144. #include <stdio.h>
  145.  
  146. main()
  147. {
  148.     char a[10][20];
  149.     char (*b)[20];
  150.     void f(char (*)[20]);
  151.  
  152.     b = a;
  153.  
  154.     f(b);
  155. }
  156.  
  157. void f(char (*a)[20])
  158. {
  159.     printf("sizeof(a) = %lu\n", 
  160.         (unsigned long) sizeof(a));
  161.     printf("sizeof(*a) = %lu\n", 
  162.         (unsigned long) sizeof(*a));
  163.     printf("sizeof(a[0]) = %lu\n", 
  164.         (unsigned long) sizeof(a[0]));
  165.     printf("sizeof(a[0][0]) = %lu\n", 
  166.         (unsigned long) sizeof(a[0][0]));
  167. }
  168.  
  169. sizeof(a) = 4
  170. sizeof(*a) = 20
  171. sizeof(a[0]) = 20
  172. sizeof(a[0][0]) = 1
  173.  
  174.  
  175.  
  176.  
  177. #include <stdio.h>
  178.  
  179. main()
  180. {
  181.     char c0;
  182.     char c1[1];
  183.     char c2[1][1];
  184.     char c3[1][1][1];
  185.  
  186.     printf("sizes are = %lu, %lu, %lu, %lu\n", 
  187.         (unsigned long) sizeof(c0),
  188.         (unsigned long) sizeof(c1),
  189.         (unsigned long) sizeof(c2),
  190.         (unsigned long) sizeof(c3));
  191. }
  192.  
  193.  
  194.  
  195.